package com.turman.coroutionsample.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.turman.coroutionsample.net.InfoItem
import com.turman.coroutionsample.paging.GithubPagingSource
import kotlinx.coroutines.flow.Flow

class PagingViewModel(app: Application) : AndroidViewModel(app) {

    fun loadData(): Flow<PagingData<InfoItem>> {
        return Pager(  //这里通过pagingsource构造pager,然后转成flow返回
            config = PagingConfig(pageSize = 10, initialLoadSize = 30),
            pagingSourceFactory = { GithubPagingSource() })
            .flow
            .cachedIn(viewModelScope)  //页面翻转可以保证数据不重新加载
    }
}